package com.jmlee.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jmlee.mapper.ItemsImgMapper;
import com.jmlee.mapper.ItemsMapper;
import com.jmlee.mapper.ItemsParamMapper;
import com.jmlee.pojo.CommonPage;
import com.jmlee.pojo.Items;
import com.jmlee.pojo.ItemsImg;
import com.jmlee.pojo.ItemsParam;
import com.jmlee.service.ItemsService;
import com.jmlee.vo.ItemDetailVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description
 * @Author jmLee
 * @Date 2021/3/9 10:15
 * @Version 1.0
 */
@Service
public class ItemsServiceImpl implements ItemsService {

    @Autowired
    private ItemsMapper itemsMapper;

    @Autowired
    private ItemsImgMapper itemsImgMapper;

    @Autowired
    private ItemsParamMapper itemsParamMapper;

    @Override
    public CommonPage<Items> getItemsList(Long cid, Integer sort, Integer curPage, Integer pageSize) {

        PageHelper.startPage(curPage, pageSize);
        List<Items> items = itemsMapper.selectItemsByCid(cid, sort);

        return getPagedResult(items, curPage);
    }

    @Override
    public CommonPage<Items> searchItem(String keyword, Integer curPage, Integer pageSize) {


        PageHelper.startPage(curPage,pageSize);
        List<Items> itemsList = itemsMapper.selectItemsByKeyword(keyword);

        return getPagedResult(itemsList,curPage);
    }

    @Override
    public ItemDetailVO getItemDetail(Long itemId) {

        Items items = itemsMapper.selectItemById(itemId);
        List<ItemsImg> itemsImgList = itemsImgMapper.selectImgByItemId(itemId);
        ItemsParam itemsParam = itemsParamMapper.selectItemParamById(itemId);
        ItemDetailVO itemDetailVO = new ItemDetailVO();
        itemDetailVO.setItems(items);
        itemDetailVO.setPics(itemsImgList);
        itemDetailVO.setItemsParam(itemsParam);

        return itemDetailVO;
    }


    /**
     * 将查询出来的列表数据进行分页包装
     *
     * @param list
     * @param page
     * @return
     */
    private CommonPage getPagedResult(List<?> list, Integer page) {

        // PageHelper 为我们提供的类
        PageInfo<?> pageList = new PageInfo<>(list);

        // 自己定义的类，为了与前端进行适配，不直接采用 PageInfo 进行返回
        CommonPage commonPage = new CommonPage();
        commonPage.setCurrentPage(page); // 当前页
        commonPage.setData(list);  // 数据
        commonPage.setTotalPage(pageList.getPages()); // 总页数
        commonPage.setCount(pageList.getTotal()); // 总记录数
        return commonPage;
    }

    /**
     * 获取某个商品信息
     *
     * @param itemId
     * @return
     */
    @Override
    public Items getItemsById(Long itemId) {

        Items items = itemsMapper.selectItemById(itemId);
        return items;
    }

    @Override
    public Long reduceStock(Long itemId, Integer buyCount) {

        return itemsMapper.reduceItemStock(itemId, buyCount);
    }

    @Override
    public Long releaseStock(Long itemId, Integer num) {

        return itemsMapper.releaseItemStock(itemId, num);
    }


    @Override
    public CommonPage<Items> getSpecialItems(Integer itemType) {
        PageHelper.startPage(1, 4);
        List<Items> itemsList = itemsMapper.selectItemsByType(itemType);

        return  getPagedResult(itemsList, 1);
    }
}
